//
//  RecommendViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RecommendViewController.h"
#import "Type_1ViewController.h"
#import "Type_2ViewController.h"
#import "Type_3ViewController.h"
#import "AboutElemData.h"
#import "AboutDataData.h"
#import "Type_1CViewCell.h"
#import "Type_2CViewCell.h"
#import "Type_3CViewCell.h"
#import "HFCollectionRView.h"
#import "UIImageView+WebCache.h"
#import "RecommendHandler.h"
#import "MBProgressHUD.h"
#import "HotViewController.h"
#import "PhotoViewController.h"
#import "UIColor+AddColor.h"
#import "MusicDataSource.h"
#import "TypeDetailViewController.h"
#import "NetworkingHandler.h"

@interface RecommendViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate,MBProgressHUDDelegate>

@property (nonatomic, retain) NSMutableArray *pOneArr;
@property (nonatomic, retain) NSMutableArray *pTwoArr;
@property (nonatomic, retain) UICollectionView *typeView2;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UICollectionView *typeView1;
@property (nonatomic, retain)MBProgressHUD *progress;
@property (nonatomic, retain) UIButton *headButton;
@end

// 设置标识
static NSString *indentify3 = @"indentify3";
static NSString *indentify2 = @"indentify2";
static NSString *indentify1 = @"indengify1";

@implementation RecommendViewController

- (void)dealloc
{
    [_pOneArr release];
    [_pTwoArr release];
    [_typeView1 release];
    [_typeView2 release];
    [_headButton release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //    CGFloat width = self.view.frame.size.width;
    //    CGFloat hight = self.view.frame.size.height;
    
    [self dataHandler];
    [self handlerNet];
    
    // 最上面不动的滚动视图
    self.tabBarController.tabBar.translucent = NO;
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorFromHexCode:@"#cde6c7"];
    
    UICollectionViewFlowLayout *flowLayout1 = [[UICollectionViewFlowLayout alloc] init];
    // 每个cell大小
    flowLayout1.itemSize = CGSizeMake(self.view.frame.size.width, 150);
    // 滚动方向
    flowLayout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    // 最小列间距
    flowLayout1.minimumLineSpacing = 0;
    // 边缘距离
    //    flowLayout1.sectionInset =UIEdgeInsetsMake(0, 0, 0, 0);
    
    _typeView1 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) collectionViewLayout:flowLayout1];
    //        _typeView1.contentSize = CGSizeMake(self.view.frame.size.width * 4, 0);
    
    // 按页翻
    _typeView1.pagingEnabled = YES;
    _typeView1.dataSource = self;
    _typeView1.delegate = self;
    _typeView1.backgroundColor = [UIColor whiteColor];
    [_typeView1 registerClass:[Type_1CViewCell class] forCellWithReuseIdentifier:indentify1];
    [self.view addSubview:_typeView1];
    [flowLayout1 release];
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 最小行间距
    flowLayout.minimumLineSpacing = 15;
    // 最小列间距
    flowLayout.minimumInteritemSpacing = 0;
    //设置，上／左／下／右边距 空间间隔数是多少
    flowLayout.sectionInset =UIEdgeInsetsMake(0, 18, 0, 18);
    //如果有多个区 就可以拉动
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //设置头部并给定大小
    //    [flowLayout setHeaderReferenceSize:CGSizeMake(_typeView.frame.size.width, 20)];
    _typeView2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height - 150 - 49 - 64) collectionViewLayout:flowLayout];
    _typeView2.dataSource = self;
    _typeView2.delegate = self;
    _typeView2.backgroundColor = [UIColor colorFromHexCode:@"#f6f5ec"];
    
    
    // 注册单元格
    [_typeView2 registerClass:[Type_3CViewCell class] forCellWithReuseIdentifier:indentify3];
    [_typeView2 registerClass:[Type_2CViewCell class] forCellWithReuseIdentifier:indentify2];
    // 注册头部视图
    [_typeView2 registerClass:[HFCollectionRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [self.view addSubview:_typeView2];
    [flowLayout release];
    
    
    self.progress = [[MBProgressHUD showHUDAddedTo:self.view animated:YES]retain];
    self.progress.delegate = self;
    [_progress show:YES];
    
}

// section大小自定义
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (collectionView == _typeView2){
        return CGSizeMake(self.view.bounds.size.width, 35);
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _typeView1) {
        return CGSizeMake(self.view.frame.size.width, 150);
    }else if (collectionView == _typeView2) {
        if (indexPath.section == 0) {
            return CGSizeMake(290, 160);
        }else {//if (indexPath.section == 1){
            return CGSizeMake(135, 135);
        }
    }
    return CGSizeZero;
}



// 有多少个Section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (collectionView == _typeView1) {
        return 1;
    }else if (collectionView == _typeView2){
        return self.pTwoArr.count;
    }
    return 0;
}

// 每个Section有多少个元素
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _typeView1) {
        
        return self.pOneArr.count;
        
    }else if (collectionView == _typeView2) {
        AboutElemData *elem = [self.pTwoArr objectAtIndex:section];
        NSLog(@"%ld", elem.data.count);
        return elem.data.count;
    }
    return 0;
}



// 每个单元格的数据引入
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _typeView1) {
        Type_1CViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentify1 forIndexPath:indexPath];
        
        MusicDataSource *musicDate = [self.pOneArr objectAtIndex:indexPath.row];
        cell.musicData = musicDate;
        
        //        AboutDataData *dataCell = [self.pOneArr objectAtIndex:indexPath.item];
        //
        //        cell.aboutData1 = dataCell;
        //        NSLog(@"**********%@", cell.aboutData1.image_url);
        return cell;
        
    }else {
        if (indexPath.section == 0) {
            //初始化每个单元格
            Type_2CViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:indentify2 forIndexPath:indexPath];
            
            AboutElemData *aboutData = [self.pTwoArr objectAtIndex:indexPath.section];
            
            cell.aboutData2 = [aboutData.data objectAtIndex:indexPath.item];
            
            return cell;
        }else{
            
            Type_3CViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentify3 forIndexPath:indexPath];
            AboutElemData *aboutData = [self.pTwoArr objectAtIndex:indexPath.section];
            
            cell.aboutData3 = [aboutData.data objectAtIndex:indexPath.item];
            
            
            return cell;
        }
    }
    
}


//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionReusableView *reusableView = nil;
    if (collectionView == _typeView1) {
        return nil;
    }else if (kind == UICollectionElementKindSectionHeader) {
        //定制头部视图的内容
        HFCollectionRView *headerV = (HFCollectionRView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"forIndexPath:indexPath];
        AboutElemData *titleData = [self.pTwoArr  objectAtIndex:indexPath.section];
        headerV.titleLabel.text = titleData.title;
        headerV.titleLabel.font = [UIFont systemFontOfSize:14];
        headerV.more.text = @"更多 >>";
        headerV.more.font = [UIFont systemFontOfSize:14];
        headerV.more.textAlignment = NSTextAlignmentRight;
        
        if (indexPath.section == 0) {
            self.headButton = [UIButton buttonWithTitle:nil frame:headerV.titleLabel.frame target:self action:@selector(buttonAction:)];
            [headerV addSubview:_headButton];
        }else{
            self.headButton = [UIButton buttonWithTitle:nil frame:headerV.titleLabel.frame target:self action:@selector(buttonActionPhoto:)];
            [headerV addSubview:_headButton];
            
        }
        reusableView = headerV;
    }
    return reusableView;
}


- (void) buttonAction:(UIButton *)button
{
    NSLog(@"头部");
    HotViewController *hotVC = [[HotViewController alloc] init];
    [self.navigationController pushViewController:hotVC animated:YES];
    [hotVC release];
    
    
    
}

- (void) buttonActionPhoto:(UIButton *)button
{
    NSLog(@"脚步");
    PhotoViewController *photoVC = [[PhotoViewController alloc] init];
    [self.navigationController pushViewController:photoVC animated:YES];
    [photoVC release];
}


//点击单元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _typeView1) {
        TypeDetailViewController *detialVC = [[TypeDetailViewController alloc] init];
        
        detialVC.mds = [self.pOneArr objectAtIndex:indexPath.item];
        
        [self.navigationController pushViewController:detialVC animated:YES];
    }
    
    if (collectionView == _typeView2) {
        if (indexPath.section == 0) {
            Type_2ViewController *type_2VC = [[Type_2ViewController alloc] init];
            AboutElemData *eleData = self.pTwoArr[indexPath.section];
            type_2VC.typeVData2 = [eleData.data objectAtIndex:indexPath.item];
            //            NSLog(@"77777777777777%@", type_2VC.typeVData2.name);
            [self.navigationController pushViewController:type_2VC animated:YES];
            
        }else {
            Type_3ViewController *type_3VC = [[Type_3ViewController alloc] init];
            AboutElemData *eleData = self.pTwoArr[indexPath.section];
            type_3VC.typeVData3 = [eleData.data objectAtIndex:indexPath.item];
            
            //            NSLog(@"6666666666666666%@", type_3VC.typeVData3.photo);
            [self.navigationController pushViewController:type_3VC animated:YES];
            
            NSLog(@"%ld",indexPath.section);
            NSLog(@"%ld", indexPath.item);
        }
    }
}



- (void)dataHandler
{// 数据
    NSString *URLstr = @"http://api.breadtrip.com/featured/";
    
    [RecommendHandler getDataWithUrl:URLstr completion:^(NSDictionary *responseObject) {
        
        
        //        self.pOneArr = [NSMutableArray array];
        self.pTwoArr = [NSMutableArray array];
        
        NSMutableArray *allTypeData = [responseObject objectForKey:@"elements"];
        for (NSMutableDictionary *dic in allTypeData) {
            AboutElemData *elemData = [[AboutElemData alloc] initWithDictionary:dic];
            
            NSMutableArray *typeTwoArr = [NSMutableArray array];
            
            NSArray *dataData = [dic objectForKey:@"data"];
            
            NSNumber *typeNum = [dic objectForKey:@"type"];
            NSString *numStr = [NSString stringWithFormat:@"%@", typeNum];
            
            
            //            if ([[dataData firstObject] isKindOfClass:[NSArray class]]) {
            //                for (NSMutableDictionary *dic in [dataData firstObject]) {
            //                    AboutDataData *dataModel = [[AboutDataData alloc] initWithDictionary:dic];
            //                    [self.pOneArr addObject:dataModel];
            //                    [dataModel release];
            //                }
            //                continue;
            //            }else
            if ([numStr isEqualToString:@"2"] || [numStr isEqualToString:@"3"]){
                for (NSMutableDictionary *dic in dataData) {
                    AboutDataData *dataModel = [[AboutDataData alloc] initWithDictionary:dic];
                    [typeTwoArr addObject:dataModel];
                    [dataModel release];
                }
                
            }else{
                continue;
            }
            elemData.data = typeTwoArr;
            [self.pTwoArr addObject:elemData];
        }
        [_typeView2 reloadData];
        [_typeView1 reloadData];
        [_progress hide:YES];
        
    }];
}

- (void)handlerNet
{
    NSString *str = @"http://mobile.ximalaya.com/m/category_tag_list?category=music&device=iPhone&type=album";
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        
        //        NSLog(@"%@",resultDic);
        
        self.pOneArr = [NSMutableArray array];
        NSArray *list = [resultDic objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            MusicDataSource *mds = [[MusicDataSource alloc] initWithDic:dic];
            if (self.pOneArr.count < 4) {
                [self.pOneArr addObject:mds];
            } else {
                break;
            }
            [mds release];
        }
        
        //        NSLog(@"%@", self.arr);
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
